﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGame4
{
    public class Movable : DrawableGameComponent
    {
        public int direction;
        public Vector2 position;
        public SpriteManager spritemanager;
        public Vector2 moveto;
        private int nilai;
        
        //constructor
        public Movable(Game1 game, int direction, Vector2 position, SpriteManager spritemanager,int nilai)
            : base(game)
        {
            this.direction = direction;
            this.position = position;
            this.spritemanager = spritemanager;
            this.moveto = position;
            this.nilai = nilai;
            setmap(nilai);
        }

        public override void Draw(GameTime gameTime)
        {
            ((Game1)Game).spriteBatch.Draw(spritemanager.GetTexture(), position, Color.White);

            base.Draw(gameTime);
        }

        public override void Update(GameTime gameTime)
        {
            if (direction == 1)
            {
                if (this.position.Y != this.moveto.Y && this.moveto.Y >= 0 && this.moveto.Y <= (600 - this.spritemanager.GetTexture().Height))
                {
                    if (!isCollision())
                    {
                        this.position.Y = this.moveto.Y;
                    }
                }
            }
            else
            {
                if (this.position.X != this.moveto.X && this.moveto.X >= 0 && this.moveto.X <= (600 - this.spritemanager.GetTexture().Width))
                {
                    if (!isCollision())
                    {
                        this.position.X = this.moveto.X;
                    }
                }
            }

            base.Update(gameTime);
        }
        
        public void setmap(int nilai)
        {
            if (direction == 1)
            {
                for (int i = 0; i < (spritemanager.GetTexture().Height) / 100; i++)
                {
                    ((Game1)Game).field.GetGrid()[(int)(position.Y / 100) + i, (int)(position.X / 100)] = nilai;
                }
            }
            else
            {
                for (int i = 0; i < (spritemanager.GetTexture().Width) / 100; i++)
                {
                    ((Game1)Game).field.GetGrid()[(int)(position.Y / 100), (int)(position.X / 100) + i] = nilai;
                }
            }
        }
        
        public bool isClick(float x, float y)
        {
            if (x > position.X && x < position.X + spritemanager.GetTexture().Width && y > position.Y && y < position.Y + spritemanager.GetTexture().Height)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        public bool isCollision()
        {
            for (int i = 0; i < ((Game1)Game).movable.Count; i++)
            {
                if (((Game1)Game).movable[i] != this)
                {
                    if (this.direction == 0)
                    {
                        if (((Game1)Game).movable[i].position.Y <= this.position.Y && ((Game1)Game).movable[i].position.Y + ((Game1)Game).movable[i].spritemanager.GetTexture().Height > this.position.Y)
                        {
                            if (((Game1)Game).movable[i].position.X < this.position.X)
                            {
                                if (((Game1)Game).movable[i].position.X + ((Game1)Game).movable[i].spritemanager.GetTexture().Width > this.moveto.X - 1)
                                    return true;
                            }
                            else if (((Game1)Game).movable[i].position.X - 1 < this.moveto.X + (this.spritemanager.GetTexture().Width))
                                return true;
                        }
                    }
                    else
                    {
                        if (((Game1)Game).movable[i].position.X <= this.position.X && ((Game1)Game).movable[i].position.X + ((Game1)Game).movable[i].spritemanager.GetTexture().Width > this.position.X)
                        {
                            if (((Game1)Game).movable[i].position.Y < this.position.Y)
                            {
                                if (((Game1)Game).movable[i].position.Y + ((Game1)Game).movable[i].spritemanager.GetTexture().Height > this.moveto.Y - 1)
                                    return true;
                            }
                            else if (((Game1)Game).movable[i].position.Y - 1 < this.moveto.Y + (this.spritemanager.GetTexture().Height))
                                return true;
                        }
                    }
                }
            }
            return false;
        }

        public void moveTo(float x, float y)
        {
            this.moveto = new Vector2(x - (spritemanager.GetTexture().Width / 2), y - (spritemanager.GetTexture().Height / 2));
        }

        public void resetmoveto()
        {
            int x = (int)((position.X + 10) / 100) * 100;
            int y = (int)((position.Y + 10) / 100) * 100;
            setmap(0);
            position = new Vector2(x, y);
            moveto = position;
            setmap(nilai);
        }

        public bool outofmap()
        {
            if ((position.X + spritemanager.GetTexture().Width) > 600 || (position.Y + spritemanager.GetTexture().Height) > 600)
            {
                return true;
            }
            else 
            {
                return false;
            }
        }
    }
}
